﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using Expression.Blend.SampleData.PanoramaBackgrounds;
using ActivityTracker.Library;
using Microsoft.Phone.Tasks;
using System.IO;
using Microsoft.Phone;
using System.Windows.Navigation;

using Common.Library;


namespace ActivityTracker
{
    public partial class BackgroundSelection : PhoneApplicationPage
    {
        public BackgroundSelection()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(BackgroundSelection_Loaded);
        }
        void BackgroundSelection_Loaded(object sender, RoutedEventArgs e)
        {
            string Nav = SettingsHelper.getStringValue("Nav");
            if (Nav == "0")
            {

                NavigationService.Navigate(new Uri("/StartActivity.xaml?SelectedBackground", UriKind.Relative));
                SettingsHelper.Save("Nav", "1");
            }
        }
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
               
                
                if (ListBoxBackgrounds.SelectedIndex != -1)
                {
                    BitmapImage bmi = (e.AddedItems[0] as Item).PanImage as BitmapImage;
                    string str = System.IO.Path.GetFileName(bmi.UriSource.OriginalString);
                    SettingsHelper.Save("backGroundName", str);

                    NavigationService.Navigate(new Uri("/StartActivity.xaml?SelectedBackground", UriKind.Relative));
             
          
                }
              
            }
            catch (Exception ex)
            {
                string mess = "Exception in ListBox_SelectionChanged Method In BackgroundSelection file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                ex.Data.Add("Date", DateTime.Now);
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            PhotoChooserTask phtochoser = new PhotoChooserTask();
            phtochoser.Completed += new EventHandler<PhotoResult>(phtochoser_Completed);
            SettingsHelper.Save("backGroundName", "user");
            phtochoser.Show();
        }
        private void phtochoser_Completed(object sender, PhotoResult e)
        {
            try
            {
                if (e.TaskResult == TaskResult.OK)
                {
                    App.ImageBytes = new byte[e.ChosenPhoto.Length];
                    e.ChosenPhoto.Read(App.ImageBytes, 0, App.ImageBytes.Length);
                    e.ChosenPhoto.Seek(0, SeekOrigin.Begin);
                    var imageSource = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
                    App.AcitivityImgSrc = imageSource;
                   
                    Dispatcher.BeginInvoke(() => this.NavigationService.Navigate(new Uri("/Views/Themes/Photo Chooser.xaml", UriKind.Relative)));
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in phtochoser_Completed Method In ChangeBackground file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }
       
    }
}
